package com.blog.servlet;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.blog.dao.ReplayDao;
import com.blog.dao.UserDao;
import com.blog.dto.Replay;
import com.blog.dto.User;
import com.blog.util.DateTime;

public class ReplayServlet extends HttpServlet {

	protected void findAllReplay(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Vector<Replay> vReplays = new Vector<Replay>();
		ReplayDao replayDao = new ReplayDao();
		vReplays = replayDao.findAllReplay();
		HttpSession session = req.getSession();
		session.setAttribute("allReplay", vReplays);
		return ;
	}
	
	protected void delReplay(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int replay_id = Integer.parseInt(req.getParameter("replay_id"));
		ReplayDao replayDao = new ReplayDao();
		if(replayDao.delReplayById(replay_id)) {
			findAllReplay(req, resp);
			req.getRequestDispatcher("/admin/OKAndClose.html").forward(req, resp);
		}else {
			resp.getWriter().append("评论删除失败!");
		}
		return ;
	}
	protected boolean login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		boolean flag = false ;
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		UserDao userDao = new UserDao();
		HttpSession session = req.getSession();
		int superstate = userDao.findUserByUsernameAndPassword(username, password);
		if(superstate == 0) {
			return false ;
		}
		else {
			User user = new User();
			user = userDao.findUserByUsername(username).get(0);
			session.setAttribute("user", user);
			session.setAttribute("userSessionID", session.getId());
			System.out.println("sessionID: "+session.getId());
//			req.getRequestDispatcher("/admin/index.jsp").forward(req, resp);
			return true ;
		}
	}
	protected void addReplay(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int article_id = Integer.parseInt(req.getParameter("article_id"));
		int status =  Integer.parseInt(req.getParameter("status"));
		String username = null;
		if(status==0) {
			if(!login(req, resp)) {
				resp.getWriter().append("<script language=\"javascript\">alert(\"用户名或密码错误，登录失败！\");</script>");
				return;
			}
		}
		username = req.getParameter("username");
		String ArticleUrl = req.getParameter("ArticleUrl");
		String content = req.getParameter("content");
		Replay replay = new Replay();
		replay.setArticle_id(article_id);
		replay.setPublish_time(DateTime.getDateTime());
		replay.setUsername(username);
		replay.setContent(content);
		ReplayDao replayDao = new ReplayDao();
		if(replayDao.addReplay(replay)) {
			findAllReplay(req, resp);
			resp.sendRedirect(req.getContextPath()+ArticleUrl);
		}else {
			resp.getWriter().append("评论删除失败!");
		}
		return ;
	}
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String act = null;
		act=req.getParameter("act");
		if(act==null || act.equals("")) act = "index";
		System.out.println("ReplayServlet act = "+act);
		
		switch (act) {
		case "del":
			delReplay(req, resp);
			break;
		case "findAllReplay":
			findAllReplay(req, resp);
			resp.sendRedirect(req.getContextPath()+"/admin/replay-list.jsp");
			break;
		case "add":
			addReplay(req, resp);
			break;

		default:
			findAllReplay(req, resp);
//			页面重定向
			resp.sendRedirect(req.getContextPath()+"/admin/replay-list.jsp");
//			页面转发
//			req.getRequestDispatcher("/index.jsp").forward(req, resp);
			break;
		}
	}

}
